Jason Freels
05 January 2018
Getting started with Git and Github
The basics of version control systems
Using Git and Github for collaborative software development
A decentralized version control system that's free and open source
An excellent way to track and coordinate file changes among many people
A filing system for every draft of a document
A tool invented by Linus Torvalds (and the Linux development community) to develop the Linux kernel in 2005
The most widely used modern version control system in the world --
Free software distributed under the terms of the GNU General Public License version 2
While mainly used for code, Git can be used to manage any type of file/project
Word documents
Final Cut projects
The entire volume of German federal laws
A code sharing and publishing service
A social networking site for programmers
The single largest web-based service for hosting Git repositories
A popular way to collaborate on code (no more emails and zip files!!)
The development versions of many R packages are hosted on GitHub: https://github.com/tidyverse/ggplot2
All the cool kids are using it!
Includes several features beyond command-line Git
A Web-based graphical interface
Task/project management tools
A martketplace of integrations for many 3rd party services
Issues!
Forking!
Workflows without Git typically consist of
Installing Git is simple - on Mac/Windows/Linux/Unix
Enter some basic information about your new account
After installing Git and setting up a Github account
global Git configuration file to tell Git who you areSince you passed the option --global, you only need to do this once!
Now your all set up to use Git & Github!
Git is one of many different version control systems (VCS)
It's important to put Git in context
You should understand what a VCS is, what types of VCS exist, and how they differ
New users often struggle learning how to use Git and conclude that it's too hard
This misconception is driven by the fact that there seems to be an endless number of use cases
The issue is NOT that Git is too hard to learn, rather Git is too big to learn
The fact that Github adds it's own functions makes things even bigger and more complicated
Remember
At the command line
Within an IDE (e.g. RStudio)
From a Git desktop GUI
Hybrid method with libgit2
git2r packagecd (change directories)git init (initialize a folder as a Git repo)git add <file-name> (start tracking a new file)git commit -m "fix typo" (commit your changes)git clone (clone/copy another Git repo [e.g., from GitHub])git push origin master (push your master branch to your origin server)git pull origin master (update your local repo) Tip: Typing git add --all will start tracking everything (this is how I almost always use git add).Initialize a repo in an existing directory
arithmetic anywhere on your computer (e.g., the Desktop)add.Radd <- function(x, y) { x * y }Next, open a terminal or Git Bash (Windows) and go to the project's root directory
For example, the code below takes me to the folder called arithmetic on my Desktop
cd C:/Users/greenweb/Desktop/arithmeticgit initThis add a new directory called .git within the arithmetic project directory
If you want to start version controlling all the files, you need to start tracking them. In the terminal, type the following:
This adds all of the files to the staging area
Then type the following to commit the file changes
Often you'll want to host a local Git repo on Github to use the collaboration tools
To do this we'll first need to create a new repo on Github
Next, we'll add the URL of the Github repo as a remote to the local repo
The remote acts like an online target for pushed changes
remoteThe function in the add.R file contained a typo
First, let's fix the obvious typo in add.R
Then, take a snapshot (i.e., git add/commit) and push the changes to GitHub (i.e., git push)!
Finally, refresh your browswer and see if it worked!
Tip: It's good practice to do this every time you make a key change (e.g., fix a typo, add a new function to an R script, etc.).
Make sure you're in the right directory!
Any questions?